package com.danny.demo.proxy.jdk.CGLIB;

import com.danny.demo.Test;
import org.mockito.cglib.proxy.Enhancer;
import org.mockito.cglib.proxy.MethodInterceptor;
import org.mockito.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName: CGLIBProxy
 * @Description: TODO
 * @Author: zhangdong
 * @Date: Created in 16:04 2019/6/26
 * @Version:
 */
public class CGLIBProxy implements MethodInterceptor {

    private  Object target;
    public CGLIBProxy(Object target){
        this.target = target;
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result = method.invoke(target,objects);
        return result;
    }

    public static Object getProxy(Object target){
        Enhancer enhancer = new Enhancer();
        //设置需要代理的对象
        enhancer.setSuperclass(target.getClass());
        //设置代理人
        enhancer.setCallback(new CGLIBProxy(target));
        return enhancer.create();
    }
}
